/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import POJO.LoaiSanPhamPOJO;
import POJO.SanPhamPOJO;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import ultil.MySqlDataAccessHelper;

/**
 *
 * @author NGUYEN CONG BINH
 */
public class SanPhamDAO {

    public static ArrayList<SanPhamPOJO> layDanhSachSanPham() {
        ArrayList<SanPhamPOJO> ds = new ArrayList<SanPhamPOJO>();

        String sql = "SELECT * FROM sanpham";
        MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
        helper.open();
        ResultSet rs = helper.executeQuery(sql);
        try {
            while (rs.next()) {
                SanPhamPOJO sp = new SanPhamPOJO();
                sp.setMaSanPham(rs.getInt("maSanPham"));
                sp.setTenSanPham(rs.getString("tenSanPham"));
                sp.setMaLoaiSanPham(rs.getInt("maLoaiSanPham"));
                sp.setMaNhaSanXuat(rs.getInt("maNhaSanXuat"));
                sp.setGiaBan(rs.getFloat("giaBan"));
                sp.setSoLuong(rs.getInt("soLuong"));
                sp.setHinhAnh(rs.getString("hinhAnh"));
                sp.setThongSo(rs.getString("thongSo"));
                sp.setDangGiamGia(rs.getInt("dangGiamGia"));
                ds.add(sp);
            }
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
        helper.close();
        return ds;
    }

    public static ArrayList<SanPhamPOJO> layDanhSachLoaiSanPham(int MaLoaiSanPham, int MaNhaSanXuat) 
    {
        ArrayList<SanPhamPOJO> ds = new ArrayList<SanPhamPOJO>();
        String SQL = "Select * from SanPham where maLoaiSanPham = " + MaLoaiSanPham +" "
                + "and MaNhaSanXuat = "+ MaNhaSanXuat;
        try{
            MySqlDataAccessHelper h = new MySqlDataAccessHelper();
            h.open();
            ResultSet rs = h.executeQuery(SQL);
            while(rs.next())
            {
                SanPhamPOJO sp = new SanPhamPOJO();
                sp.setMaSanPham(rs.getInt("maSanPham"));
                sp.setTenSanPham(rs.getString("tenSanPham"));
                sp.setMaLoaiSanPham(rs.getInt("maLoaiSanPham"));
                sp.setMaNhaSanXuat(rs.getInt("maNhaSanXuat"));
                sp.setGiaBan(rs.getFloat("giaBan"));
                sp.setSoLuong(rs.getInt("soLuong"));
                sp.setHinhAnh(rs.getString("hinhAnh"));
                sp.setThongSo(rs.getString("thongSo"));
                sp.setDangGiamGia(rs.getInt("dangGiamGia"));
                ds.add(sp);
            }
            h.close();
        }
        catch(Exception ex){}
        return ds;
    }
//     private static ArrayList<SanPhamPOJO> layDanhSachSanPhamCoDanhMuc(ResultSet rs) throws SQLException {
//        ArrayList<SanPhamPOJO> ds = new ArrayList<SanPhamPOJO>();
//        while (rs.next()) {
//            SanPhamPOJO sp = new SanPhamPOJO();
//            sp.setMaSanPham(rs.getInt("maSanPham"));
//            sp.setTenSanPham(rs.getString("tenSanPham"));
//            sp.setMaLoaiSanPham(rs.getInt("maLoaiSanPham"));
//            sp.setMaNhaSanXuat(rs.getInt("maNhaSanxuat"));
//            sp.setGiaBan(rs.getFloat("giaBan"));
//            sp.setSoLuong(rs.getInt("soLuong"));
//            sp.setHinhAnh(rs.getString("hinhAnh"));
//            ds.add(sp);
//        }
//        return ds;
//     }
//    public static ArrayList<SanPhamPOJO> laySanPhamTheoDanhMuc(String maDanhMuc) {
//        ArrayList<SanPhamPOJO> ds = new ArrayList<SanPhamPOJO>();
//        String sql = String.format("SELECT * FROM SanPham"
//                + " WHERE maDanhMuc='%s'",
//                maDanhMuc);
//        try {
//            MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
//            helper.open();
//            ResultSet rs = helper.executeQuery(sql);
//            ds = layDanhSachSanPhamCoDanhMuc(rs);
//            helper.close();
//        } catch (SQLException ex) {
//            System.out.println(ex.getMessage());
//        }
//        return ds;
//    }

}